package com.gzx.plugin.website.configs.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gzx.plugin.website.configs.bo.GzxWebsiteConfigAddOrEditBo;
import com.gzx.plugin.website.configs.bo.GzxWebsiteConfigQueryBo;
import com.gzx.plugin.website.configs.entity.GzxWebsiteConfig;
import com.gzx.plugin.website.configs.mapper.GzxWebsiteConfigMapper;
import com.gzx.plugin.website.configs.service.GzxWebsiteConfigService;
import com.gzx.plugin.website.configs.vo.GzxWebsiteConfigOutVo;
import com.gzx.plugin.website.configs.vo.GzxWebsiteConfigVo;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.mvc.service.impl.BaseServiceImpl;
import vip.xiaonuo.common.tenant.TenantContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zjt
 * @description
 * @date 2025/4/23 11:08
 */
@Service
@DS("website")
public class GzxWebsiteConfigServiceImpl extends BaseServiceImpl<GzxWebsiteConfigAddOrEditBo, GzxWebsiteConfigVo, GzxWebsiteConfigQueryBo, GzxWebsiteConfigMapper, GzxWebsiteConfig> implements GzxWebsiteConfigService {
    public GzxWebsiteConfigServiceImpl(GzxWebsiteConfigMapper baseMapper) {
        super(baseMapper);
    }

    private final Map<String, String> companyDictMap = new HashMap<>();

    @Override
    protected LambdaQueryWrapper<GzxWebsiteConfig> buildQueryWrapper(GzxWebsiteConfigQueryBo queryBo) {
        return Wrappers.<GzxWebsiteConfig>lambdaQuery().orderByDesc(GzxWebsiteConfig::getId);
    }

    /**
     * 获取公司官网配置
     *
     * @param host
     * @return
     */
    @Override
    public GzxWebsiteConfigOutVo getConfigByHost(String host) {
        GzxWebsiteConfig websiteConfig = baseMapper.selectOne(Wrappers.<GzxWebsiteConfig>lambdaQuery().eq(GzxWebsiteConfig::getHost, host));
        if (websiteConfig == null) {
            return null;
        }
        GzxWebsiteConfigOutVo websiteConfigOutVo = new GzxWebsiteConfigOutVo();
        BeanUtils.copyProperties(websiteConfig, websiteConfigOutVo);
        return websiteConfigOutVo;
    }

    /**
     * 根据Host获取配置ID
     *
     * @param host
     * @return
     */
    @Override
    public GzxWebsiteConfig getConfigIdByHost(String host) {
        return baseMapper.selectOne(Wrappers.<GzxWebsiteConfig>lambdaQuery()
                .eq(GzxWebsiteConfig::getHost, host));
    }

    /**
     * 获取所有的调解中心
     *
     * @return
     */
    @Override
    public List<GzxWebsiteConfigVo> listAll() {
        return baseMapper.selectVoList(Wrappers.<GzxWebsiteConfig>lambdaQuery()
                .eq("1944637731787374594".equals(StpLoginUserUtil.getLoginUser().getOrgId()), GzxWebsiteConfig::getHost, "www.tjjrtj.com"));
    }

    /**
     * 获取公司字典映射
     *
     * @return
     */
    @Override
    public Map<String, String> getCompanyDict() {
        return companyDictMap;
    }

    @Scheduled(fixedDelay = 600000)
    public void fetchData() {
        try {
            TenantContext.disableTenantFilter();
            List<GzxWebsiteConfig> websiteConfigList = baseMapper.selectList(Wrappers.<GzxWebsiteConfig>lambdaQuery()
                    .select(GzxWebsiteConfig::getId, GzxWebsiteConfig::getCompanyName));
            for (GzxWebsiteConfig websiteConfig : websiteConfigList) {
                companyDictMap.put(String.valueOf(websiteConfig.getId()), websiteConfig.getCompanyName());
            }
        } finally {
            TenantContext.enableTenantFilter();
            TenantContext.clear();
        }
    }
}
